﻿Imports System.Data.OleDb

Public Class PlayerDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim playerID As String = Request.Params("PlayerID")

        If Not String.IsNullOrEmpty(playerID) Then

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            oleDbConn.Open()

            Dim selectPlayerSQL As String = "SELECT FullName FROM Players WHERE ID=@f1"
            Dim selectPlayerCmd As OleDbCommand = New OleDbCommand(selectPlayerSQL, oleDbConn)
            selectPlayerCmd.CommandType = CommandType.Text
            selectPlayerCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
            Dim selectPlayerDr As String = selectPlayerCmd.ExecuteScalar()

            If Not String.IsNullOrEmpty(selectPlayerDr) Then

                lblPlayerName.Text = selectPlayerDr
                selectPlayerCmd.Dispose()

                txtUsername.Text = User.Identity.Name

                If Not IsPostBack Then
                    'Check already liked player
                    Dim selectPlayerLikeSQL As String = "SELECT * FROM PlayerLikes WHERE Username=@f1 AND PlayerID_FK=@f2"
                    Dim selectPlayerLikeCmd As New OleDbCommand(selectPlayerLikeSQL, oleDbConn)
                    selectPlayerLikeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
                    selectPlayerLikeCmd.Parameters.AddWithValue("@f2", Request.Params("PlayerID"))
                    Dim selectPlayerLikeDr As OleDbDataReader = selectPlayerLikeCmd.ExecuteReader()

                    If (selectPlayerLikeDr.HasRows()) Then
                        btnPlayerLike.Visible = False
                        btnPlayerUnlike.Visible = True
                    Else
                        btnPlayerLike.Visible = True
                        btnPlayerUnlike.Visible = False
                    End If

                    selectPlayerLikeCmd.Dispose()

                    Dim selectUserLikeSQL As String = "SELECT PlayerUserLikes FROM PlayerUserLikes WHERE FullName = @f1"
                    Dim selectUserLikeCmd As OleDbCommand = New OleDbCommand(selectUserLikeSQL, oleDbConn)
                    selectUserLikeCmd.CommandType = CommandType.Text
                    selectUserLikeCmd.Parameters.AddWithValue("@f1", lblPlayerName.Text)
                    Dim selectUserLikeDr As Int32 = selectUserLikeCmd.ExecuteScalar()

                    If selectUserLikeDr = 1 And btnPlayerUnlike.Visible Then
                        lblPlayerLikes.Text = "You like this"
                    ElseIf selectUserLikeDr = 1 And btnPlayerLike.Visible Then
                        lblPlayerLikes.Text = "1 person likes this"
                    ElseIf selectUserLikeDr = 2 And btnPlayerUnlike.Visible Then
                        lblPlayerLikes.Text = "You and another person like this"
                    ElseIf selectUserLikeDr = 2 And btnPlayerLike.Visible Then
                        lblPlayerLikes.Text = "2 people like this"
                    ElseIf selectUserLikeDr > 2 And btnPlayerUnlike.Visible Then
                        lblPlayerLikes.Text = "You and " & (selectUserLikeDr - 1) & " others like this"
                    ElseIf selectUserLikeDr > 2 And btnPlayerLike.Visible Then
                        lblPlayerLikes.Text = selectUserLikeDr & " people like this"
                    Else
                        lblPlayerLikes.Text = "Be the first to like this!"
                    End If

                    selectUserLikeCmd.Dispose()

                    Dim selectTagsSQL As String = "SELECT * FROM Tags"
                    Dim selectTagsCmd As OleDbCommand = New OleDbCommand(selectTagsSQL, oleDbConn)
                    selectTagsCmd.CommandType = CommandType.Text
                    Dim selectTagsDr = selectTagsCmd.ExecuteReader()
                    cblTags.DataSource = selectTagsDr
                    cblTags.DataBind()
                    selectTagsCmd.Dispose()

                    Dim websiteUser As String

                    If Not String.IsNullOrEmpty(User.Identity.Name) Then
                        websiteUser = User.Identity.Name
                    Else
                        websiteUser = "Anonymous"
                    End If

                    Dim createPlayerViewSQL As String = "INSERT INTO PlayerViews(Username,PlayerID_FK,ViewedAt) VALUES (@f1,@f2,@f3)"
                    Dim createPlayerViewCmd As OleDbCommand = New OleDbCommand(createPlayerViewSQL, oleDbConn)
                    createPlayerViewCmd.CommandType = CommandType.Text
                    createPlayerViewCmd.Parameters.AddWithValue("@f1", websiteUser)
                    createPlayerViewCmd.Parameters.AddWithValue("@f2", Request.Params("PlayerID"))
                    createPlayerViewCmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
                    createPlayerViewCmd.ExecuteNonQuery()
                    createPlayerViewCmd.Dispose()

                    Dim selectPlayerViewSQL As String = "SELECT PlayerUserViews FROM PlayerUserViews WHERE FullName = @f1"
                    Dim selectPlayerViewCmd As OleDbCommand = New OleDbCommand(selectPlayerViewSQL, oleDbConn)
                    selectPlayerViewCmd.CommandType = CommandType.Text
                    selectPlayerViewCmd.Parameters.AddWithValue("@f1", lblPlayerName.Text)
                    Dim selectPlayerViewDr As Int32 = selectPlayerViewCmd.ExecuteScalar()
                    lblPlayerViews.Text = "Views: " & selectPlayerViewDr
                    selectPlayerViewCmd.Dispose()

                    Dim selectPlayerCommentsSQL As String = "SELECT * FROM PlayerComments WHERE PlayerID_FK = @f1"
                    Dim selectPlayerCommentsCmd As OleDbCommand = New OleDbCommand(selectPlayerCommentsSQL, oleDbConn)
                    selectPlayerCommentsCmd.CommandType = CommandType.Text
                    selectPlayerCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
                    Dim selectPlayerCommentsDr = selectPlayerCommentsCmd.ExecuteReader()
                    gvPlayerComments.DataSource = selectPlayerCommentsDr
                    gvPlayerComments.DataBind()

                    'If gvPlayerComments.Rows.Count = 0 Then
                    '    phNoComments.Visible = True
                    'Else
                    '    phNoComments.Visible = False
                    'End If

                    selectPlayerCommentsCmd.Dispose()

                    'Check already tagged names by user
                    Dim selectPlayerTagsSQL As String = "SELECT TagID_FK FROM PlayerTags WHERE PlayerID_FK=@f1 AND username=@f2"
                    Dim selectPlayerTagsCmd As OleDbCommand = New OleDbCommand(selectPlayerTagsSQL, oleDbConn)
                    selectPlayerTagsCmd.CommandType = CommandType.Text
                    selectPlayerTagsCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
                    selectPlayerTagsCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
                    Dim selectPlayerTagsDr = selectPlayerTagsCmd.ExecuteReader()

                    If selectPlayerTagsDr.HasRows Then
                        Do While selectPlayerTagsDr.Read()
                            For Each cb As ListItem In cblTags.Items
                                If selectPlayerTagsDr.GetInt32(0) = cb.Value Then
                                    cb.Selected = True
                                End If
                            Next
                        Loop
                    End If
                    selectPlayerTagsCmd.Dispose()

                    'Check already rated clubs by user
                    Dim selectPlayerRatingSQL As String = "SELECT Rating FROM PlayerRatings WHERE PlayerID_FK=@f1 AND Username=@f2"
                    Dim selectPlayerRatingCmd As OleDbCommand = New OleDbCommand(selectPlayerRatingSQL, oleDbConn)
                    selectPlayerRatingCmd.CommandType = CommandType.Text
                    selectPlayerRatingCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
                    selectPlayerRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)

                    Dim selectPlayerRatingDr As Int32 = Convert.ToInt32(selectPlayerRatingCmd.ExecuteScalar())
                    RatingForPlayer.CurrentRating = selectPlayerRatingDr
                    selectPlayerRatingCmd.Dispose()
                End If

            Else
                Session("UserMessage_Error") = "The specified player does not exist!"
                Response.Redirect("~/ClubList.aspx")
            End If

            oleDbConn.Close()
            oleDbConn.Dispose()

        Else
            Session("UserMessage_Error") = "A player must be specified!"
            Response.Redirect("~/ClubList.aspx")
        End If

        If String.IsNullOrEmpty(User.Identity.Name) Then
            btnPlayerLike.Visible = False
            btnPlayerUnlike.Visible = False
            pnlPlayerLikes.Visible = False
            phRatings.Visible = False
            phTags.Visible = False
            phComments.Visible = False
            pnlTagAnonymous.Visible = True
            pnlCommentAnonymous.Visible = True
            pnlRating.Visible = True
            RatingForPlayer.Visible = False
        Else
            pnlRating.Visible = False
            pnlTagAnonymous.Visible = False
            pnlCommentAnonymous.Visible = False
            RatingForPlayer.Visible = True
        End If

    End Sub

    Protected Sub btnBackClubPlayers_Click(sender As Object, e As EventArgs) Handles btnBackClubPlayers.Click
        Dim clubID As String = dvHiddenClubID.Rows(0).Cells(1).Text
        Response.Redirect("~/PlayerList.aspx?ClubID=" + clubID)
    End Sub

    Protected Sub btnAddComment_Click(sender As Object, e As EventArgs) Handles btnAddComment.Click
        If Page.IsValid Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            oleDbConn.Open()

            Dim insertPlayerCommentSQL As String = "INSERT INTO PlayerComments(Username,Title,Comment,PlayerID_FK,CommentDateTime) VALUES (@f1,@f2,@f3,@f4,@f5)"
            Dim insertPlayerCommentCmd As OleDbCommand = New OleDbCommand(insertPlayerCommentSQL, oleDbConn)
            insertPlayerCommentCmd.CommandType = CommandType.Text
            insertPlayerCommentCmd.Parameters.AddWithValue("@f1", txtUsername.Text)
            insertPlayerCommentCmd.Parameters.AddWithValue("@f2", txtTitle.Text)
            insertPlayerCommentCmd.Parameters.AddWithValue("@f3", txtComment.Text)
            insertPlayerCommentCmd.Parameters.AddWithValue("@f4", Request.Params("PlayerID"))
            insertPlayerCommentCmd.Parameters.AddWithValue("@f5", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
            insertPlayerCommentCmd.ExecuteNonQuery()
            insertPlayerCommentCmd.Dispose()

            Dim selectPlayerCommentSQL As String = "SELECT * FROM PlayerComments WHERE PlayerID_FK = @f1"
            Dim selectPlayerCommentCmd As OleDbCommand = New OleDbCommand(selectPlayerCommentSQL, oleDbConn)
            selectPlayerCommentCmd.CommandType = CommandType.Text
            selectPlayerCommentCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
            Dim dataReader = selectPlayerCommentCmd.ExecuteReader()
            gvPlayerComments.DataSource = dataReader
            gvPlayerComments.DataBind()
            selectPlayerCommentCmd.Dispose()

            oleDbConn.Close()
            oleDbConn.Dispose()

            txtTitle.Text = ""
            txtComment.Text = ""

        Else
            Session("UserMessage_Error") = "There was a problem with the data you submitted. Please try again."
            Response.Redirect(Request.RawUrl)
        End If
    End Sub

    Protected Sub btnTagPlayer_Click(sender As Object, e As EventArgs) Handles btnTagPlayer.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim existingTagsSQL As String = "SELECT TagID_FK FROM PlayerTags WHERE TagID_FK=@f1 AND PlayerID_FK=@f2 AND username=@f3"

        For Each cb As ListItem In cblTags.Items

            'Check if already exists in DB
            Dim existingTagsCmd As OleDbCommand = New OleDbCommand(existingTagsSQL, oleDbConn)
            existingTagsCmd.CommandType = CommandType.Text
            existingTagsCmd.Parameters.AddWithValue("@f1", cb.Value)
            existingTagsCmd.Parameters.AddWithValue("@f2", Request.Params("PlayerID"))
            existingTagsCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
            Dim existingTagsDr = existingTagsCmd.ExecuteReader()
            existingTagsCmd.Dispose()

            If cb.Selected Then
                If Not existingTagsDr.HasRows Then
                    Dim selectPlayerTagsSQL As String = "INSERT INTO PlayerTags(PlayerID_FK,TagID_FK,Username,TaggedAt) VALUES (@f1,@f2,@f3,@f4)"
                    Dim selectPlayerTagsCmd As OleDbCommand = New OleDbCommand(selectPlayerTagsSQL, oleDbConn)
                    selectPlayerTagsCmd.CommandType = CommandType.Text
                    selectPlayerTagsCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
                    selectPlayerTagsCmd.Parameters.AddWithValue("@f2", cb.Value)
                    selectPlayerTagsCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
                    selectPlayerTagsCmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
                    selectPlayerTagsCmd.ExecuteNonQuery()
                    selectPlayerTagsCmd.Dispose()
                    lblTaggingDone.Text = "Player tags were successfully recorded!"
                End If
            Else
                If existingTagsDr.HasRows Then
                    Dim removePlayerTagsSQL As String = "DELETE FROM PlayerTags WHERE PlayerID_FK=@f1 AND TagID_FK=@f2 AND Username=@f3"
                    Dim removePlayerTagsCmd As OleDbCommand = New OleDbCommand(removePlayerTagsSQL, oleDbConn)
                    removePlayerTagsCmd.CommandType = CommandType.Text
                    removePlayerTagsCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
                    removePlayerTagsCmd.Parameters.AddWithValue("@f2", cb.Value)
                    removePlayerTagsCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
                    removePlayerTagsCmd.ExecuteNonQuery()
                    removePlayerTagsCmd.Dispose()
                    lblTaggingDone.Text = "Player tags were successfully recorded!"
                End If
            End If
        Next

        If Not String.IsNullOrEmpty(txtTags.Text) Then
            Dim newTags = txtTags.Text.Split(",")

            For Each newTag In newTags
                'First add the new tag to the tag table
                Dim insertTagSQL As String = "INSERT INTO Tags(TagName) VALUES(@f1)"
                Dim insertTagCmd As OleDbCommand = New OleDbCommand(insertTagSQL, oleDbConn)
                insertTagCmd.CommandType = CommandType.Text
                insertTagCmd.Parameters.AddWithValue("@f1", newTag)
                insertTagCmd.ExecuteNonQuery()
                insertTagCmd.Dispose()

                'Then find the ID of the new tag
                Dim returnLastIDSQL As String = "SELECT TOP 1 ID FROM Tags ORDER BY ID DESC"
                Dim returnLastIDCmd As OleDbCommand = New OleDbCommand(returnLastIDSQL, oleDbConn)
                Dim returnLastIDDr = returnLastIDCmd.ExecuteReader()
                returnLastIDDr.Read()
                Dim lastID = returnLastIDDr.GetValue(0)
                returnLastIDCmd.Dispose()

                'Associate this last ID with the Player ID
                Dim insertPlayerTagSQL As String = "INSERT INTO PlayerTags(PlayerID_FK,TagID_FK,Username,TaggedAt) VALUES (@f1,@f2,@f3,@f4)"
                Dim insertPlayerTagCmd As OleDbCommand = New OleDbCommand(insertPlayerTagSQL, oleDbConn)
                insertPlayerTagCmd.CommandType = CommandType.Text
                insertPlayerTagCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
                insertPlayerTagCmd.Parameters.AddWithValue("@f2", lastID)
                insertPlayerTagCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
                insertPlayerTagCmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
                insertPlayerTagCmd.ExecuteNonQuery()
                insertPlayerTagCmd.Dispose()
                lblTaggingDone.Text = "Player tags were successfully recorded!"
            Next

            txtTags.Text = ""

            Dim allTagsSQL As String = "SELECT * FROM Tags"
            Dim allTagsCmd As OleDbCommand = New OleDbCommand(allTagsSQL, oleDbConn)
            allTagsCmd.CommandType = CommandType.Text
            Dim allTagsDr = allTagsCmd.ExecuteReader()
            cblTags.DataSource = allTagsDr
            cblTags.DataBind()
            allTagsCmd.Dispose()

            'Check already tagged names by user
            Dim selectPlayerTagsSQL As String = "SELECT TagID_FK FROM PlayerTags WHERE PlayerID_FK=@f1 AND username=@f2"
            Dim selectPlayerTagsCmd As OleDbCommand = New OleDbCommand(selectPlayerTagsSQL, oleDbConn)
            selectPlayerTagsCmd.CommandType = CommandType.Text
            selectPlayerTagsCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
            selectPlayerTagsCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim selectPlayerTagsDr = selectPlayerTagsCmd.ExecuteReader()

            If selectPlayerTagsDr.HasRows Then
                Do While selectPlayerTagsDr.Read()
                    For Each cb As ListItem In cblTags.Items
                        If selectPlayerTagsDr.GetInt32(0) = cb.Value Then
                            cb.Selected = True
                        End If
                    Next
                Loop
            End If
            selectPlayerTagsCmd.Dispose()
        End If

        System.Threading.Thread.Sleep(500)

        oleDbConn.Close()
        oleDbConn.Dispose()

    End Sub

    Protected Sub btnPlayerLike_Click(sender As Object, e As EventArgs) Handles btnPlayerLike.Click
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim insertPlayerLikeSQL As String = "INSERT INTO PlayerLikes (Username,PlayerID_FK,LikedAt) VALUES (@f1,@f2,@f3)"
        Dim insertPlayerLikeCmd As New OleDbCommand(insertPlayerLikeSQL, oleDbConn)
        insertPlayerLikeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        insertPlayerLikeCmd.Parameters.AddWithValue("@f2", Request.Params("PlayerID"))
        insertPlayerLikeCmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
        insertPlayerLikeCmd.ExecuteNonQuery()
        insertPlayerLikeCmd.Dispose()

        btnPlayerLike.Visible = False
        btnPlayerUnlike.Visible = True

        Dim selectUserLikeSQL As String = "SELECT PlayerUserLikes FROM PlayerUserLikes WHERE FullName = @f1"
        Dim selectUserLikeCmd As OleDbCommand = New OleDbCommand(selectUserLikeSQL, oleDbConn)
        selectUserLikeCmd.CommandType = CommandType.Text
        selectUserLikeCmd.Parameters.AddWithValue("@f1", lblPlayerName.Text)
        Dim selectUserLikeDr As Int32 = selectUserLikeCmd.ExecuteScalar()

        If selectUserLikeDr = 1 And btnPlayerUnlike.Visible Then
            lblPlayerLikes.Text = "You like this"
        ElseIf selectUserLikeDr = 1 And btnPlayerLike.Visible Then
            lblPlayerLikes.Text = "1 person likes this"
        ElseIf selectUserLikeDr = 2 And btnPlayerUnlike.Visible Then
            lblPlayerLikes.Text = "You and another person like this"
        ElseIf selectUserLikeDr = 2 And btnPlayerLike.Visible Then
            lblPlayerLikes.Text = "2 people like this"
        ElseIf selectUserLikeDr > 2 And btnPlayerUnlike.Visible Then
            lblPlayerLikes.Text = "You and " & (selectUserLikeDr - 1) & " others like this"
        ElseIf selectUserLikeDr > 2 And btnPlayerLike.Visible Then
            lblPlayerLikes.Text = selectUserLikeDr & " people like this"
        Else
            lblPlayerLikes.Text = "Be the first to like this!"
        End If

        selectUserLikeCmd.Dispose()

        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btnPlayerUnlike_Click(sender As Object, e As EventArgs) Handles btnPlayerUnlike.Click
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim removePlayerLikeSQL As String = "DELETE FROM PlayerLikes WHERE Username=@f1 AND PlayerID_FK=@f2"
        Dim removePlayerLikeCmd As New OleDbCommand(removePlayerLikeSQL, oleDbConn)
        removePlayerLikeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        removePlayerLikeCmd.Parameters.AddWithValue("@f2", Request.Params("PlayerID"))
        removePlayerLikeCmd.ExecuteNonQuery()
        removePlayerLikeCmd.Dispose()

        btnPlayerLike.Visible = True
        btnPlayerUnlike.Visible = False

        Dim selectUserLikeSQL As String = "SELECT PlayerUserLikes FROM PlayerUserLikes WHERE FullName = @f1"
        Dim selectUserLikeCmd As OleDbCommand = New OleDbCommand(selectUserLikeSQL, oleDbConn)
        selectUserLikeCmd.CommandType = CommandType.Text
        selectUserLikeCmd.Parameters.AddWithValue("@f1", lblPlayerName.Text)
        Dim selectUserLikeDr As Int32 = selectUserLikeCmd.ExecuteScalar()

        If selectUserLikeDr = 1 And btnPlayerUnlike.Visible Then
            lblPlayerLikes.Text = "You like this"
        ElseIf selectUserLikeDr = 1 And btnPlayerLike.Visible Then
            lblPlayerLikes.Text = "1 person likes this"
        ElseIf selectUserLikeDr = 2 And btnPlayerUnlike.Visible Then
            lblPlayerLikes.Text = "You and another person like this"
        ElseIf selectUserLikeDr = 2 And btnPlayerLike.Visible Then
            lblPlayerLikes.Text = "2 people like this"
        ElseIf selectUserLikeDr > 2 And btnPlayerUnlike.Visible Then
            lblPlayerLikes.Text = "You and " & (selectUserLikeDr - 1) & " others like this"
        ElseIf selectUserLikeDr > 2 And btnPlayerLike.Visible Then
            lblPlayerLikes.Text = selectUserLikeDr & " people like this"
        Else
            lblPlayerLikes.Text = "Be the first to like this!"
        End If

        selectUserLikeCmd.Dispose()

        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btnEditPlayer_Click(sender As Object, e As EventArgs) Handles btnEditPlayer.Click
        Response.Redirect("~/EditPlayer.aspx?PlayerID=" + Request.Params("PlayerID"))
    End Sub

    Protected Sub RatingForPlayer_Changed(sender As Object, e As AjaxControlToolkit.RatingEventArgs)
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim removePlayerRatingSQL As String = "DELETE FROM PlayerRatings WHERE PlayerID_FK=@f1 AND Username=@f2"
        Dim removePlayerRatingCmd As New OleDbCommand(removePlayerRatingSQL, oleDbConn)
        removePlayerRatingCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
        removePlayerRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        removePlayerRatingCmd.ExecuteNonQuery()
        removePlayerRatingCmd.Dispose()

        Dim addPlayerRatingSQL As String = "INSERT INTO PlayerRatings (PlayerID_FK,Rating,Username,RatingDateTime) VALUES (@f1,@f2,@f3,@f4)"
        Dim addPlayerRatingCmd As New OleDbCommand(addPlayerRatingSQL, oleDbConn)
        addPlayerRatingCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))
        addPlayerRatingCmd.Parameters.AddWithValue("@f2", RatingForPlayer.CurrentRating)
        addPlayerRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        addPlayerRatingCmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
        addPlayerRatingCmd.ExecuteNonQuery()
        lblRatingDone.Text = "Thank you for your rating!"
        addPlayerRatingCmd.Dispose()

        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btnSignIn_Rate_Click(sender As Object, e As EventArgs) Handles btnSignIn_Rate.Click
        Response.Redirect("~/Account/Login?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl))
    End Sub

    Protected Sub btnRegister_Rate_Click(sender As Object, e As EventArgs) Handles btnRegister_Rate.Click
        Response.Redirect("~/Account/Register")
    End Sub

    Protected Sub btnSignIn_Tag_Click(sender As Object, e As EventArgs) Handles btnSignIn_Tag.Click
        Response.Redirect("~/Account/Login?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl))
    End Sub

    Protected Sub btnRegister_Tag_Click(sender As Object, e As EventArgs) Handles btnRegister_Tag.Click
        Response.Redirect("~/Account/Register")
    End Sub

    Protected Sub btnSignIn_Comment_Click(sender As Object, e As EventArgs) Handles btnSignIn_Comment.Click
        Response.Redirect("~/Account/Login?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl))
    End Sub

    Protected Sub btnRegister_Comment_Click(sender As Object, e As EventArgs) Handles btnRegister_Comment.Click
        Response.Redirect("~/Account/Register")
    End Sub

End Class